package net.xujialiang.XTestRPA.Controller.TenantManager;


import net.xujialiang.XTestRPA.Model.ResultData;
import net.xujialiang.XTestRPA.Service.Tenant.XRPATenantService;
import net.xujialiang.XTestRPA.Service.XRPAUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

import static net.xujialiang.XTestRPA.Enum.ReturnCode.RC999;

@RestController
@RequestMapping("/anonymous/api/ttc")
public class Register {
    private static final Logger logger = LoggerFactory.getLogger(Register.class);

    @Autowired
    XRPATenantService tenantSvc;
    @Autowired
    XRPAUserService userSvc;

    // 租户注册
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ResponseBody
    @Transactional
    public ResultData<Map<String, Object>> register(@RequestBody Map<String, Object> reqBody, HttpServletRequest request) {
        try{
            String ttc = (String) reqBody.get("ttc");
            String username = (String) reqBody.get("username");
            String password = (String) reqBody.get("password");
            String nickName = (String) reqBody.get("nickName");
            // 1. 创建租户
            tenantSvc.createTenant(ttc, username);
            // 2. 创建用户
            userSvc.createuser(username, password, ttc, nickName, false);
            return ResultData.success();
        }catch (Exception e){
            return ResultData.fail(RC999.getCode(), e.getMessage());
        }
    }
}
